#include "ResultFormatter.h"
#include "QueryTree.h"

void Format(ResultTable* p_result_table_i, list<string>& results_o)
{
	// case of BOOLEAN result
	if( p_result_table_i == NULL )
		return;
	// case of non BOOLEAN result
	vector< vector<int> >* p_results = p_result_table_i->get_table_data();
	for( vector< vector<int> >::iterator iter_v_int = p_results->begin();
		iter_v_int != p_results->end();
		iter_v_int++ )
	{
		stringstream ss_result_entry;
		switch(QueryVarTable::GetQueryVarType( p_result_table_i->get_query_var_idxes()->at(0) ))
		{
		case PROC_TYPE:
			ss_result_entry << ProcTable::GetProcName((*iter_v_int)[0]);
			break;
		case VAR_TYPE:
			ss_result_entry << VarTable::GetVarName((*iter_v_int)[0]);
			break;
		case CALL_TYPE:
			if(QueryTree::get_pv_query_var_type()->at(0)){
				ss_result_entry << ProcTable::GetProcName(StmtTable::GetStmtTNode((*iter_v_int)[0])->value);
			}else{
				ss_result_entry << (*iter_v_int)[0];
			}
			break;
		default:
			ss_result_entry << (*iter_v_int)[0];
		}

		for( unsigned int i = 1; i < (*iter_v_int).size(); i++ )
		{
			switch(QueryVarTable::GetQueryVarType( p_result_table_i->get_query_var_idxes()->at(i) ))
			{
			case PROC_TYPE:
				ss_result_entry << " " << ProcTable::GetProcName((*iter_v_int)[i]);
				break;
			case VAR_TYPE:
				ss_result_entry << " " << VarTable::GetVarName((*iter_v_int)[i]);
				break;
			case CALL_TYPE:
				if(QueryTree::get_pv_query_var_type()->at(i)){
					ss_result_entry << " " << ProcTable::GetProcName(StmtTable::GetStmtTNode((*iter_v_int)[i])->value);
				}else{
					ss_result_entry << " " << (*iter_v_int)[i];
				}
				break;
			default:
				ss_result_entry << " " << (*iter_v_int)[i];
			}
		}
		results_o.push_back(ss_result_entry.str());
	}
}